%To write the source current files 

% for polar gyre computations

%latest date 14.5.6 Potsdam Rehbrueke !

MyInPath = 'c:\Manoj\ocean\Velocities\monthly\';
MyOutPath = 'c:\Manoj\ocean\polar\';



if isdir(MyInPath) == 0,

    display('In path does not exist');

    return;

end;

if isdir(MyOutPath) == 0,

    mkdir('c:\Manoj\ocean\','polar\');

end;



S = dir(MyInPath);



nfiles = length(S);

ndatafile=1;

for i = 1:nfiles,

    if length(S(i).name) == 8,

        FileName(ndatafile,:) = S(i).name;

        ndatafile = ndatafile+1;

    end;

end;


i =1;

disp([MyInPath FileName(i,:)]);
fid = fopen([MyInPath FileName(i,:)],'r','b');
IC = fread(fid,[180,360],'float');
ICU = fread(fid,[180,360],'float');
ICV= fread(fid,[180,360],'float');
IU = fread(fid,[180,360],'float');
IV = fread(fid,[180,360],'float');
Bz = fread(fid,[180,360],'float');
fclose(fid);

 

IU(1:10,:) = -IU(141:150,:); % Ficticious Anti-cyclonic gyre at Polar region
CU = -IV.*Bz.*1e-9.*3.2;
CV = -IU.*Bz.*1e-9.*3.2;
CU = circshift(CU,[0,180]);
CV = circshift(CV,[0,180]);

%CU = CU*1000; % Compression for e format 20 oct 2005
%CV = CV*1000; % This multiplication is compensated by scale factor in source file
%fp = fopen([MyOutPath 'head.txt'],'wt');
%fprintf(fp,'%s\n',['    4000.           ' Polar ' ' Polar '.source' ' 2']);

fid = fopen([MyOutPath 'Polar' '.source'],'wt');
fid1 = fopen('c:\manoj\ocean\x3dg\header.txt','rt');

%the following format for alexei's x3dg is tested ok 20 oct 05

           for ii = 1:34,

                s=fgetl(fid1);

                fprintf(fid,'%s\n',s);

            end;

            s=fgetl(fid1);

for in = 1:180,

    for jn = 1:360,

       fprintf(fid,'%12.4e\n',CU(in,jn));

   end;

end;

            fprintf(fid,'\n');

            for ii = 1:17,

            s=fgetl(fid1);

            fprintf(fid,'%s\n',s);

            end;

            s=fgetl(fid1);

for in = 1:180,

    for jn = 1:360,

        fprintf(fid,'%12.4e\n',0);

    end;

end;

            fprintf(fid,'\n');

            for ii = 1:17,

            s=fgetl(fid1);

            fprintf(fid,'%s\n',s);

            end;

            s=fgetl(fid1);

 for in = 1:180,

    for jn = 1:360,       

        fprintf(fid,'%12.4e\n',CV(in,jn));

    end;

end;

            fprintf(fid,'\n');

            for ii = 1:17,

            s=fgetl(fid1);

            fprintf(fid,'%s\n',s);

            end;

            s=fgetl(fid1);

  

 for in = 1:180,

    for jn = 1:360,       

        fprintf(fid,'%12.4e\n',0);

    end;

end;

            fprintf(fid,'\n');

            for ii = 1:37,

            s=fgetl(fid1);

            fprintf(fid,'%s\n',s);

            end;

            s=fgetl(fid1);



           fclose(fid);

           fclose(fid1);

fclose all;